package com.ns.scs.xf;

import com.ns.basic.log.utils.LogUtil;
import com.ns.scs.xf.dto.XfDto;
import com.ns.scs.xf.listener.XFSocketListener;
import com.ns.scs.xf.model.Text;
import com.ns.scs.xf.utils.XFAuthUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * 讯飞工具类
 *
 * @author liaowei
 */
@Slf4j
@RequiredArgsConstructor
public class TestXF {
    /**
     * string专用
     */
    public static final String HOST_URL = "https://spark-api.xf-yun.com/v4.0/chat";
    public static final String APP_ID = "64112601";
    public static final String API_SECRET = "NzY1NmVhMmE3NzE0YTRiMDE2NDUyNDY4";
    public static final String API_KEY = "4d29c1aff36b85e66c0b6b3acbf6d2ce";
    public static final String DOMAIN = "4.0Ultra";
    private static final String USER_ID = "10000";
    /**
     * 角色
     */
    private static final String ROLE = "user";

    /**
     * max_tokens
     */
    private static final Integer MAX_TOKENS = 4098;

    /**
     * 随机性
     */
    private static final Float TEMPERATURE = 0.5f;
    /**
     * 灵活度
     */
    private static final Integer TOP_K = 3;
    /**
     * 该参数仅4.0 Ultra版本支持，当设置为true时，如果输入内容触发联网检索插件，会先返回检索信源列表，然后再返回星火回复结果，否则仅返回星火回复结果
     */
    private static final Boolean SHOW_REF_LABEL = Boolean.TRUE;


    private static final Boolean IS_CONTINUE = Boolean.TRUE;

    // 主函数
    public static void main(String[] args) throws Exception {
        ArrayList<Text> list = new ArrayList<>();
        // 个性化参数入口
        while (IS_CONTINUE) {
            // 鉴权url
            String authUrl = XFAuthUtil.getAuthUrl(HOST_URL, API_KEY, API_SECRET);
            OkHttpClient client = new OkHttpClient.Builder().build();
            Request request = new Request.Builder().url(authUrl).build();
            for (int i = 0; i < 1; i++) {
                // 输入
                LogUtil.info("我：");

                XfDto listener = getXfListener();
                client.newWebSocket(request, new XFSocketListener(listener, list));
            }
        }
    }

    /**
     * 封装 lister参数
     *
     * @return XFListener
     */
    private static @NotNull XfDto getXfListener() {
        Scanner scanner = new Scanner(System.in);
        String question = scanner.nextLine();
        XfDto listener = new XfDto();
        listener.setUserId(USER_ID);
        listener.setAppId(APP_ID);
        listener.setDomain(DOMAIN);
        listener.setQuestion(question);
        listener.setRole(ROLE);
        listener.setMaxTokens(MAX_TOKENS);
        listener.setTemperature(TEMPERATURE);
        listener.setTopK(TOP_K);
        listener.setShowRefLabel(SHOW_REF_LABEL);
        return listener;
    }
}
